package com.xnx3.banmahuishou.qishouApplet.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.Daishoudian;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.AttachmentUtil;
import com.xnx3.j2ee.util.SafetyUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.j2ee.vo.UploadFileVO;

/**
 * 骑手端--代收点
 * @author qyw
 */
@Controller
@RequestMapping("/qishouApplet/daishoudian")
public class DaishoudianController extends BaseController {

	
	@Resource
	private SqlService sqlService;
	
	/**
	 * 骑手端--添加代收点
	 * @param dsdName		代收点名称
	 * @param dsdUsername	联系人姓名
	 * @param dsdPhone		手机号
	 * @param dsdAddress	详细地址
	 * @param lat			纬度
	 * @param lon			经度
	 * @param remark		备注
	 * @param image		代收点门头照片
	 * @param sheng			省
	 * @param shi			市
	 * @param qu			区
	 * @author qyw
	 */
	@RequestMapping(value = "/addDaishoudian.json",method = RequestMethod.POST)
	@ResponseBody
	public BaseVO addDaishoudian(HttpServletRequest request, 
			@RequestParam(value = "dsdName", required = false,defaultValue = "") String dsdName,
			@RequestParam(value = "dsdUsername", required = false,defaultValue = "") String dsdUsername,
			@RequestParam(value = "dsdPhone", required = false,defaultValue = "") String dsdPhone,
			@RequestParam(value = "dsdAddress", required = false,defaultValue = "") String dsdAddress,
			@RequestParam(value = "lat", required = false,defaultValue = "0") double lat,
            @RequestParam(value = "lon", required = false,defaultValue = "0") double lon,
        	@RequestParam(value = "remark", required = false,defaultValue = "") String remark,
        	@RequestParam(value = "image", required = false,defaultValue = "") String image,
        	@RequestParam(value = "sheng", required = false,defaultValue = "") String sheng,
        	@RequestParam(value = "shi", required = false,defaultValue = "") String shi,
        	@RequestParam(value = "qu", required = false,defaultValue = "") String qu) {
		
		int userId = getUserId();
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		if (qishou == null) {
			return notLogin();
		}
		if (!StringUtils.hasLength(dsdName)) {
			return error("请填写代收点名称");
		}
		if (!StringUtils.hasLength(dsdUsername)) {
			return error("请填写代收点联系人名称");
		}
		if (!StringUtils.hasLength(dsdPhone)) {
			return error("请填写代收点联系手机号");
		}
		if (!StringUtils.hasLength(dsdAddress)) {
			return error("请填写代收点详细地址");
		}
		if (lat <= 0 || lon <= 0) {
			return error("请填写经纬度");
		}
		if (!StringUtils.hasLength(image)) {
			return error("请上传代收点门头照");
		}
		
		Daishoudian daishoudian = new Daishoudian();
		daishoudian.setCreateTime(DateUtil.timeForUnix10());
		daishoudian.setDaishoudianAddress(SafetyUtil.xssFilter(dsdAddress));
		daishoudian.setDaishoudianImage(image);
		daishoudian.setDaishoudianName(SafetyUtil.xssFilter(dsdName));
		daishoudian.setDaishoudianPhone(SafetyUtil.xssFilter(dsdPhone));
		daishoudian.setDaishoudianUsername(SafetyUtil.xssFilter(dsdUsername));
		daishoudian.setLatitude(lat);
		daishoudian.setLongitude(lon);
		daishoudian.setQishouPhone(qishou.getPhone());
		daishoudian.setQishouUserid(qishou.getId());
		daishoudian.setQishouUsername(qishou.getName());
		daishoudian.setQu(qu);
		daishoudian.setRemark(SafetyUtil.xssFilter(remark));
		daishoudian.setSheng(sheng);
		daishoudian.setShi(shi);
		daishoudian.setState(Daishoudian.STATE_REVIEW);
		daishoudian.setDailishangUserid(qishou.getDailishangUserid());
		daishoudian.setWanggeUserid(qishou.getWanggeUserid());
		
		sqlService.save(daishoudian);
		ActionLogUtil.insert(request, userId, "骑手端--添加代收点 :" + daishoudian.toString());
		return success();
	}
	
	
	
	/**
	 * 骑手端---添加代收点时上传图片
	 * @param multipartFile 图片文件
	 * @author qyw
	 */
	@RequestMapping(value="/upImage.json", method= {RequestMethod.POST})
	@ResponseBody
	public UploadFileVO upImage(HttpServletRequest request, 
			@RequestParam(value = "multipartFile",required = true,defaultValue = "") MultipartFile multipartFile){

		UploadFileVO vo = new UploadFileVO();
		int userid = getUserId();
		if(userid == 0){
			vo.setBaseVO(BaseVO.FAILURE, "请登录");
			return vo;
		}
		//将图片上传到 file/partner/ 路径下
		vo = AttachmentUtil.uploadFile("file/daishoudian/", multipartFile); 
		if (vo.getUrl() == null) {
			vo.setBaseVO(BaseVO.FAILURE, "上传图片失败");
			return vo;
		}
		// 记录日志
		ActionLogUtil.insertUpdateDatabase(request, userid, "骑手端---添加代收点时上传图片", "图片 :"+vo.getUrl()); 
		return vo;
	}
	
	
	
}
